package com.jeect.productweb.service;

import com.jeect.base.domain.Product;
import com.jeect.productweb.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ProductRemoteService productRemoteService;

    @Override
    public Product getFromLocalCacheBySku(String sku) {
        RedisUtil redisUtil = new RedisUtil(redisTemplate);

        Product product = (Product) redisUtil.hget("products", sku);
        return product;
    }

    @Override
    public Product getBySku(String sku) {
        Product product = this.getFromLocalCacheBySku(sku);
        System.out.println("product========="+product);
        if(product==null){
            product =  this.productRemoteService.getFromRemoteBySku(sku);
            System.out.println("product_cache========="+product);
        }
        return product;
    }

    /*@Override
    //Feign调用失败后调用此方法
    public Product getFromRemoteBySku(String sku) {
        System.out.println("接口调用失败........");
        return null;
    }*/
}
